package cc.sunwl.demo.controller;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cc.sunwl.demo.entity.Demo;
import cc.sunwl.demo.util.CommonUtils;

@Controller
@RequestMapping("demo")
public class DemoController {

    private static final Logger log = LoggerFactory.getLogger(DemoController.class);

    @GetMapping("get")
    @ResponseBody
    public Demo getText(HttpServletRequest request) {
        String ipAddress = CommonUtils.getIpAddress(request);
        log.info("访问的ip为：{}", ipAddress);
        Demo demo = new Demo();
        demo.setId(1L);
        demo.setName("test");
        return demo;
    }

    @PostMapping("postForm")
    @ResponseBody
    public Demo postForm(Demo demo) {
        log.info("对象的id：{}, name：{}", demo.getId(), demo.getName());
        return demo;
    }

    @PostMapping("postJson")
    @ResponseBody
    public Demo postJson(@RequestBody Demo demo) {
        log.info("对象的id：{}, name：{}", demo.getId(), demo.getName());
        return demo;
    }
}
